﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetJson;
using FS.Common.DotNetUI;
using FS.Common.DotNetData;
using FS.Business;
using System;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Text;

namespace FS.Web.Frame
{
    /// <summary>
    /// Frame 的摘要说明
    /// </summary>
    public class Frame : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public bool Islogin(HttpContext context, string User_Account)
        {
            return true;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1.0);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            string Action = context.Request["action"];
            string user_Account = context.Request["user_Account"];
            string userPwd = context.Request["userPwd"];
            string code = context.Request["code"]; 
           
            System_Dao sys_dal = new System_Dao();
            IPScanerHelper objScan = new IPScanerHelper();
            string text = Action;
            if (text != null)
            {
                if (text == "Menu")
                {
                    string UserId = RequestSession.GetSessionUser().UserId.ToString() ;
                    string strMenus = JsonHelper.DataTableToJson(sys_dal.GetMenuHtml(UserId), "MENU");
                    context.Response.Write(strMenus);
                    context.Response.End();
                }
               
                else if (text == "LoginWithUncheck")
                {
                    RequestSession.AddSessionUser(new SessionUser
                    {
                        UserId = "99999",
                        UserAccount = "nologin",
                        UserName = "匿名用户",
                        UserPwd = ""
                    });                    
                }
                else if (text == "login")
                {
                    //if (code.ToLower() != context.Session["dt_session_code"].ToString().ToLower())
                    //{
                    //    context.Response.Write("1");
                    //    context.Response.End();
                    //}

                    DataTable dtlogin = sys_dal.UserLogin(user_Account.Trim(), userPwd.Trim());
                    if (dtlogin != null)
                    {
                        string ipaddress = RequestHelper.GetIP();
                        objScan.DataPath = context.Server.MapPath("/Themes/IPScaner/QQWry.Dat");
                        objScan.IP = ipaddress;
                        string OWNER_address = objScan.IPLocation();
                        if (dtlogin.Rows.Count != 0)
                        {
                            sys_dal.SysLoginLog(user_Account, "0", OWNER_address);
                            if (dtlogin.Rows[0]["DeleteMark"].ToString() == "1")
                            {
                                if (this.Islogin(context, user_Account))
                                {
                                    RequestSession.AddSessionUser(new SessionUser
                                    {
                                        UserId = dtlogin.Rows[0]["User_ID"].ToString(),
                                        UserAccount = dtlogin.Rows[0]["User_Account"].ToString(),
                                        UserName = dtlogin.Rows[0]["User_Name"].ToString()+ "(" + dtlogin.Rows[0]["User_Account"].ToString() + ")",
                                        UserDispName = dtlogin.Rows[0]["User_Name"].ToString(),
                                        UserPwd = dtlogin.Rows[0]["User_Pwd"].ToString(),
                                        UserIP = ipaddress,
                                        isViewAll = dtlogin.Rows[0]["isViewAll"].ToString(),
                                        isViewSupply = dtlogin.Rows[0]["isViewSupply"].ToString(),
                                        isViewPrice = dtlogin.Rows[0]["isViewPrice"].ToString(),
                                        isViewCost = dtlogin.Rows[0]["isViewCost"].ToString()
                                    });
                                    context.Response.Write("3");
                                    context.Response.End();
                                }
                                else
                                {
                                    context.Response.Write("6");
                                    context.Response.End();
                                }
                            }
                            else
                            {
                                //account locking
                                sys_dal.SysLoginLog(user_Account, "1", OWNER_address);
                                context.Response.Write("2");
                                context.Response.End();
                            }
                        }
                        else
                        {
                            //account no find
                            sys_dal.SysLoginLog(user_Account, "2", OWNER_address);
                            context.Response.Write("4");
                            context.Response.End();
                        }                    
                    }
                    else
                    {
                        context.Response.Write("5");
                        context.Response.End();
                    } 
                }
            }
        }
        
    }
}